import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/config/app_colors.dart';
import 'package:flutter_app/model/video_model.dart';
import 'package:flutter_app/util/util.dart';
import 'package:video_player/video_player.dart';

import 'avatar_role_name.dart';

class VideoCard extends StatefulWidget {
  final VideoItem videoItem;
  final StreamController streamController; //全局流控制器,用来监听控制视频播放器
  //ListView是否在滑动
  final bool isScroll;

  VideoCard(
      {Key key, this.videoItem, this.streamController, this.isScroll //是否播放
      });

  @override
  _VideoState createState() => _VideoState();
}

class _VideoState extends State<VideoCard> {
  VideoPlayerController _controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = VideoPlayerController.network(widget.videoItem.videoUrl)
      ..initialize().then((value) {
        setState(() {});
      });
    _controller.addListener(() {
      if (isCheck && !_controller.value.isPlaying) {
        isCheck = false;
        setState(() {});
      }
    });
  }

//  child: Stack(
//  fit: StackFit.expand,
//  children: [
//  ClipRRect(
//  borderRadius: BorderRadius.circular(10),
//  child: FadeInImage.assetNetwork(
//  placeholder: 'assets/images/common/lazy-3.png',
//  image: widget.videoItem.coverPictureUrl,
//  fit: BoxFit.cover,
//  ),
//  ),
//  Center(
//  child: Image.asset(
//  'assets/images/icons/play_plus.png',
//  width: 38,
//  height: 38,
//  ),
//  ),
//  ],
//  ),
  bool isCheck = false; //true 播放 false不播放
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Stack(
          children: [
            AspectRatio(
              //比例
              aspectRatio: 9 / 16,
              child: VideoPlayer(_controller),
            ),
            //编辑透明文字
            buildText(),
          ],
        ),
        Padding(
          padding: EdgeInsets.symmetric(vertical: 8),
          child: AvatarRoleName(
            avatar: widget.videoItem.coverPictureUrl,
            nickName: widget.videoItem.user.nickname,
            showType: false,
            type: widget.videoItem.user.type,
            avatarSize: 26,
          ),
        ),
        Row(
          children: [
            _iconText('assets/images/icons/comment.png',
                widget.videoItem.commentCount),
            _iconText(
                'assets/images/icons/like.png', widget.videoItem.thumbUpCount),
            _iconText(
                'assets/images/icons/read.png', widget.videoItem.readCount)
          ],
        ),
      ],
    );
  }

  play() {
    isCheck = !isCheck;
    if (isCheck) {
      //点击开始按钮开始播放
      if (widget.streamController != null) {
        widget.streamController.add(_controller);
      }

      //视频当前进度
      Duration position = _controller.value.position;
      //视频总长度
      Duration duration = _controller.value.duration;
      //当播放完成之后,返回到初始位置
      if (position == duration) {
        _controller.seekTo(Duration.zero);
      }
      _controller.play();
    } else {
      //暂停播放
      _controller.pause();
    }
    setState(() {});
    print("点击了");
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }

  //编辑透明文字
  Widget buildText() {
    return Positioned.fill(
      //透明动画
      child: AnimatedOpacity(
        //当 按钮文字问"开始"时,不显示当前文字
        opacity: isCheck ? 0 : 1,
        duration: new Duration(seconds: 1),
        child: GestureDetector(
          onTap: play,
          child: Container(
            color: Colors.black,
            child: Center(
              child: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
                color: Colors.red,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

Widget _iconText(String icon, int count) {
  return Expanded(
    child: Row(
      children: [
        Image.asset(
          icon,
          width: 18,
          height: 18,
        ),
        Expanded(
          child: Text(
            formatCharCount(count),
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(color: AppColors.un3active, fontSize: 13),
          ),
        ),
      ],
    ),
  );
}

onItemClick() {
  print('hh');
}
